//
//  MapViewController.m
//  SmartLife
//
//  Created by Lawrence on 15/10/12.
//  Copyright © 2015年 Lawrence. All rights reserved.
//

#import "MapViewController.h"
#import <AMapSearchKit/AMapSearchKit.h>
#import "POIAnnotation.h"

#define converturl @"http://restapi.amap.com/v3/assistant/coordinate/convert?key=897210d6f68d6a63cfc84f86d96ec5e3&coordsys=gps&locations="

#define APIKey @""

#define GeoPlaceHolder @"Searh or enter an address"


@interface MapViewController ()<MKMapViewDelegate,AMapSearchDelegate,UISearchBarDelegate,UISearchControllerDelegate,UITableViewDataSource,UITableViewDelegate>{
    
    
    CLLocation *_currentLocation;  //当前坐标
    CLLocation *_convertLocation;  //转换后的坐标
    AMapSearchAPI *_search;
    UISearchBar *_searchBar;
    
    UISearchController *_displayController;
    
    NSMutableArray *_tips;
}
@property(nonatomic,strong) MKMapView *mapView;
//@property(nonatomic,strong) AMapSearchAPI *search;
@end

@implementation MapViewController
-(instancetype)initWithMap{
    UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:@"MapView" bundle:nil];
    MapViewController *mapView=[storyBoard instantiateViewControllerWithIdentifier:@"MapView"];
    return mapView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initLocation];
    [self initMapView];
    //[self initSearchBar];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark 申请定位权限
-(void)initLocation{
    if (nil==locationManager) {
        locationManager=[[CLLocationManager alloc] init];
    }
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        [locationManager requestAlwaysAuthorization];
    }
}

-(void) initMapView{
    _mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0,21,CGRectGetWidth(self.view.bounds),CGRectGetHeight(self.view.bounds))];
    _mapView.delegate=self;
    _mapView.showsUserLocation=YES;   //显示定位图标
    [_mapView setUserTrackingMode:MKUserTrackingModeFollow]; //设置定位模式
    [self.view addSubview:_mapView];
}


-(void)initSearch{
    
}

-(void)initSearchBar{
    _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(44,0,CGRectGetWidth(self.navigationController.navigationBar.bounds)-44,44)];
    _searchBar.barStyle=UIBarStyleBlackOpaque;
    _searchBar.translucent=YES;
    _searchBar.delegate=self;
    _searchBar.placeholder=GeoPlaceHolder;
    _searchBar.keyboardType=UIKeyboardTypeDefault;
    [self.navigationController.navigationBar addSubview:_searchBar];
}

-(void)initSearchDisplay{
    _displayController=[[UISearchController alloc] initWithSearchResultsController:nil];
    _displayController.delegate=self;
//    _displayController.searchResultsController=self;
//    _displayController.searchBar=_searchBar;
}

/* 逆转地理编码 搜索*/
-(void)reverseGeocoding{
    AMapReGeocodeSearchRequest *request=[[AMapReGeocodeSearchRequest alloc] init];
    request.location=[AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
    [_search AMapReGoecodeSearch:request];
}
-(void)searchPOIWithKey:(NSString *) key adcode:(NSString *)adcode{
    if (key.length==0) {
        return;
    }
    //AMapPlaceSearchRequest *place=[[AMapPlaceSearchRequest alloc] init];0   // return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tips.count;
}
-(void)viewDidDisappear:(BOOL)animated{
    [_searchBar removeFromSuperview];
}
@end
